package com.itqf.yun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itqf.yun.dto.CommonsDto;
import com.itqf.yun.dto.SaveDto;
import com.itqf.yun.dto.UserDto;
import com.itqf.yun.mapper.UserMapper;
import com.itqf.yun.pojo.User;
import com.itqf.yun.service.UserService;
import com.itqf.yun.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;


/**
 * projectName: springboot_part
 *
 * @author: Ayangcx
 * time: 2021/9/26 14:28
 * description:
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper;

	@Override
	public R login (UserDto userDto) {
		if (StringUtils.isEmpty(userDto.getAccount())
				|| StringUtils.isEmpty(userDto.getUpwd())) {
			return R.FAIL("账号或者密码为null");
		}
		String userpwd = DigestUtils.md5DigestAsHex(userDto.getUpwd().getBytes());

		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("account", userDto.getAccount());
		queryWrapper.eq("upwd", userpwd);
		User user = userMapper.selectOne(queryWrapper);
		if (user == null) {
			return R.FAIL("账号或者密码错误");
		}
		return R.OK("登录成功", user);
	}

	@Override
	public R save (SaveDto saveDto) {

		if (StringUtils.isEmpty(saveDto.getAccount())
				|| StringUtils.isEmpty(saveDto.getUpwd())) {
			return R.FAIL("账号或者密码为null");
		}
		String userpwd = DigestUtils.md5DigestAsHex(saveDto.getUpwd().getBytes());
		User user = new User();
		user.setAccount(saveDto.getAccount());
		user.setUpwd(userpwd);
		user.setUname(saveDto.getUname());
		user.setUphone(saveDto.getUphone());
		user.setUmark(saveDto.getUmark());
		user.setUstate(user.getUstate());
		int rows = userMapper.insert(user);
		if (rows == 0) {
			return R.FAIL("添加用户失败");
		}
		return R.OK("添加用户成功");
	}

	@Override
	public R list () {
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("udeleted", 0);
		List<User> users = userMapper.selectList(queryWrapper);
		return R.OK("查询成功！", users);
	}

	@Override
	public R remove (int uid) {
		User user = new User();
		user.setUid(uid);
		user.setUdeleted(1);
		userMapper.updateById(user);
		int rows = userMapper.updateById(user);

		if (rows == 0) {
			return R.FAIL("用户删除失败！");
		}
		return R.OK("用户删除成功！");
	}

	@Override
	public R update (User user) {

//		user.setAccount(null);
//		user.setUdeleted(null);
//		user.setUpwd(null);

		int rows = 0;
		try {
			rows = userMapper.updateById(user);
		} catch (Exception e) {

		}

		if (rows == 0) {
			return R.FAIL("数据格式错误，修改失败！");
		}
		return R.OK("用户信息修改成功！");
	}

	@Override
	public R change(CommonsDto commonsDto) {
		String pwd=DigestUtils.md5DigestAsHex(commonsDto.getOld().getBytes());
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("account", commonsDto.getAccount());
		queryWrapper.eq("upwd", pwd);
		User user = userMapper.selectOne(queryWrapper);
		//2.无数据，密码错误
		if (user == null) {
			return R.FAIL("原密码错误！");
		}
		//3.有数据，修改密码
		String pwd_now=DigestUtils.md5DigestAsHex(commonsDto.getNow().getBytes());
		user.setUpwd(pwd_now);
		//修改密码不影响账号
		user.setAccount(null);
		userMapper.updateById(user);
		return R.OK("密码修改成功！");

	}

}
